<template>
  <div class="login">
    <div class="left">
      <div class="top">
        <img src="@/assets/img/login-logo.png" alt />
        <span class="t1">黑马面面</span>
        <div class="line"></div>
        <span class="t2">用户登录</span>
      </div>
      <el-form ref="form" :model="form" class="form" :rules="rules">
        <!-- 用户名 -->
        <el-form-item prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>
        <!-- 验证码 -->
        <el-form-item prop="code">
          <el-row>
            <el-col :span="18">
              <el-input v-model="form.code" placeholder="请输入验证码" prefix-icon="el-icon-key"></el-input>
            </el-col>
            <el-col :span="6">
              <img :src="baseURL+codeImg" alt class="code" @click="getImg" v-if="isFlag" />
            </el-col>
          </el-row>
        </el-form-item>
        <!-- 用户协议 -->
        <el-form-item class="check" prop="isPass">
          <el-checkbox v-model="form.isPass">
            我已阅读并同意
            <el-link type="primary">用户协议</el-link>和
            <el-link type="primary">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <!-- 登录按钮 -->
        <el-form-item>
          <el-button type="primary" class="btn" @click="goLogin">登录</el-button>
        </el-form-item>
        <!-- 注册按钮 -->
        <el-form-item>
          <el-button type="primary" class="btn" @click="getReg">注册</el-button>
        </el-form-item>
      </el-form>
      <register ref="register"></register>
    </div>
    <div class="right">
      <img src="@/assets/img/login-bg.png" alt />
    </div>
  </div>
</template>
<script>
import register from "./register.vue";
import { userLogin } from "@/api/login.js";
import { seaveLocal,getLocal } from "@/utils/local.js";
export default {
  data() {
    return {
      isFlag: true,
      codeImg: "/captcha?type=login",
      baseURL: process.env.VUE_APP_URL,
      form: {
        phone: "",
        password: "",
        code: "",
        isPass: "",
      },
      rules: {
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              let _reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (_reg.test(value)) {
                callback();
              } else {
                callback(new Error("请输入正确的手机号码"));
              }
            },
            trigger: "change",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 12, message: "请输入6到12位的密码", trigger: "blur" },
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          { min: 4, max: 4, message: "请输入4位验证码", trigger: "blur" },
        ],
        isPass: [
          { required: true, message: "请勾选协议", trigger: "change" },
          {
            validator: (rule, value, callback) => {
              if (value) {
                callback();
              } else {
                callback(new Error("请勾选协议"));
              }
            },
          },
        ],
      },
    };
  },
  components: {
    register,
  },
  methods: {
    getImg() {
      this.isFlag = false;
      this.$nextTick(() => {
        this.isFlag = true;
      });
    },
    goLogin() {
      this.$refs.form.validate((result) => {
        if (result) {
          userLogin(this.form)
            .then((res) => {
              seaveLocal("token", res.data.data.token);
              this.$router.push("/layout");
            })
            .catch(() => {
              this.getImg();
            });
        } else {
          this.$message.error("失败，请检查信息是否填写完整");
        }
      });
    },
    getReg() {
      this.$refs.register.isShow = true;
    },
  },
  created() {
    if(getLocal('token')){
      this.$router.push('/layout')
    }
    this.getImg();
  },
};
</script>

<style lang="less">
.login {
  display: flex;
  height: 100%;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  justify-content: space-around;
  align-items: center;
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 42px;
    .top {
      display: flex;
      align-items: center;
      img {
        width: 22px;
        height: 17px;
      }
      .t1 {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
        margin: 0 15px;
      }
      .line {
        width: 1px;
        height: 28px;
        background: #c7c7c7;
      }
      .t2 {
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
        margin-left: 15px;
      }
    }
    .form {
      margin-top: 30px;
      .code {
        width: 100%;
        height: 40px;
      }
      .check {
        display: flex;
        align-items: center;
      }
      .btn {
        width: 100%;
      }
    }
  }
  .right {
    width: 633px;
    height: 435px;
  }
}
</style>